/*
 * ViewPasswordDialog.java
 *
 * Created on May 21, 2012, 4:19:45 PM
 */
package org.passwordstore.gui;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import org.passwordstore.Resource;
import org.passwordstore.util.Password;

/**
 *
 * @author thotheolh
 */
public class ViewPasswordDialog extends javax.swing.JDialog {

    Password pass;

    public ViewPasswordDialog(Password currentPass) {
        pass = currentPass;
        initComponents();
        setSize(400, 190);
        userTf1.setText(pass.getUname());
        pwdTf1.setText(pass.getUpass());
        viewRQ.setText(pass.getRplace());
        pack();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bottomPane1 = new javax.swing.JPanel();
        btnPane1 = new javax.swing.JPanel();
        clearBtn1 = new javax.swing.JButton();
        updateBtn = new javax.swing.JButton();
        delBtn = new javax.swing.JButton();
        contentPane1 = new javax.swing.JPanel();
        usrLbl1 = new javax.swing.JLabel();
        pwdLbl1 = new javax.swing.JLabel();
        rqLbl1 = new javax.swing.JLabel();
        pwdTf1 = new javax.swing.JTextField();
        userTf1 = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        viewRQ = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bottomPane1.setLayout(new java.awt.BorderLayout());

        clearBtn1.setText("Clear");
        clearBtn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearBtn1ActionPerformed(evt);
            }
        });
        btnPane1.add(clearBtn1);

        updateBtn.setText("Update");
        updateBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateBtnActionPerformed(evt);
            }
        });
        btnPane1.add(updateBtn);

        delBtn.setText("Delete");
        delBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delBtnActionPerformed(evt);
            }
        });
        btnPane1.add(delBtn);

        bottomPane1.add(btnPane1, java.awt.BorderLayout.EAST);

        getContentPane().add(bottomPane1, java.awt.BorderLayout.SOUTH);

        usrLbl1.setText("Username: ");

        pwdLbl1.setText("Password: ");

        rqLbl1.setText("Required Place: ");

        pwdTf1.setColumns(20);
        pwdTf1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdTf1KeyPressed(evt);
            }
        });

        userTf1.setColumns(20);
        userTf1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userTf1KeyPressed(evt);
            }
        });

        viewRQ.setColumns(20);
        viewRQ.setLineWrap(true);
        viewRQ.setRows(5);
        viewRQ.setWrapStyleWord(true);
        jScrollPane3.setViewportView(viewRQ);

        javax.swing.GroupLayout contentPane1Layout = new javax.swing.GroupLayout(contentPane1);
        contentPane1.setLayout(contentPane1Layout);
        contentPane1Layout.setHorizontalGroup(
            contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPane1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rqLbl1)
                    .addComponent(pwdLbl1)
                    .addComponent(usrLbl1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(userTf1)
                    .addComponent(pwdTf1)
                    .addComponent(jScrollPane3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        contentPane1Layout.setVerticalGroup(
            contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(contentPane1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usrLbl1)
                    .addComponent(userTf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdLbl1)
                    .addComponent(pwdTf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(contentPane1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rqLbl1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(contentPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void clearBtn1ActionPerformed(java.awt.event.ActionEvent evt) {
        clear();
    }

    private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {
        if (Resource.getInstance().getSql().updatePassword(new Password(userTf1.getText(), pwdTf1.getText(), viewRQ.getText()), pass)) {
            JOptionPane.showMessageDialog(this, "Password updated successfully", "Password updated", JOptionPane.INFORMATION_MESSAGE);
            Resource.getInstance().refreshTables();
        } else {
            JOptionPane.showMessageDialog(this, "Password update failed.", "Password update failed", JOptionPane.ERROR_MESSAGE);
        }
        clear();
    }

    private void delBtnActionPerformed(java.awt.event.ActionEvent evt) {
        deletePassword(userTf1.getText(), viewRQ.getText());
        clear();
        this.setVisible(false);
    }

    private void pwdTf1KeyPressed(java.awt.event.KeyEvent evt) {
        if ((evt.getKeyChar() == KeyEvent.VK_TAB) || (evt.getKeyCode() == KeyEvent.VK_ENTER)) {
            viewRQ.grabFocus();
        }
    }

    private void userTf1KeyPressed(java.awt.event.KeyEvent evt) {
        if ((evt.getKeyChar() == KeyEvent.VK_TAB) || (evt.getKeyCode() == KeyEvent.VK_ENTER)) {
            pwdTf1.grabFocus();
        }
    }

    public void clear() {
        userTf1.setText("");
        pwdTf1.setText("");
        viewRQ.setText("");
    }

    /**
     * Delete password information set
     * @param username
     * @param rqplace
     */
    private void deletePassword(String username, String rqplace) {
        int numOpt = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this password set ?", "Delete Password", JOptionPane.YES_NO_OPTION);
        if (numOpt == JOptionPane.YES_OPTION) {
            pass = new Password();
            pass.setUname(username);
            pass.setRplace(rqplace);
            if (Resource.getInstance().getSql().deletePassword(pass)) {
                JOptionPane.showMessageDialog(this, "Password have been successfully deleted.", "Pasword deleted", JOptionPane.INFORMATION_MESSAGE);
                Resource.getInstance().refreshTables();
            } else {
                JOptionPane.showMessageDialog(this, "Password delete have failed.", "Pasword delete failed", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bottomPane1;
    private javax.swing.JPanel btnPane1;
    private javax.swing.JButton clearBtn1;
    private javax.swing.JPanel contentPane1;
    private javax.swing.JButton delBtn;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel pwdLbl1;
    private javax.swing.JTextField pwdTf1;
    private javax.swing.JLabel rqLbl1;
    private javax.swing.JButton updateBtn;
    private javax.swing.JTextField userTf1;
    private javax.swing.JLabel usrLbl1;
    private javax.swing.JTextArea viewRQ;
    // End of variables declaration//GEN-END:variables
}
